package cn.kpi.common.Excel.util;

import java.lang.reflect.Method;

/**
 * Create by liuyong on 2017/11/3
 */
public class ReflectionUtils {

    /**
     * 获取object所对应的method的返回值
     * @param object
     * @param methodName
     * @return
     * @throws Exception
     */
    public static Object executeMethod(Object object, String methodName)
            throws Exception {
        Class clazz = object.getClass();
        Method method = clazz.getMethod(methodName, new Class[0]);
        return method.invoke(object, new Object[0]);
    }

    public static Object executeSetMethod(Object object, String methodName,Object value)
            throws Exception {
        Class clazz = object.getClass();
        if(value instanceof Float){
            Method method = clazz.getMethod(methodName, Float.class);
            return method.invoke(object, value);
        }
        if(value instanceof String){
            Method method = clazz.getMethod(methodName, String.class);
            return method.invoke(object, value);
        }
        return "";
    }

    /**
     * 根据property返回getXXX method name
     * @param property
     * @return
     */
    public static String returnGetMethodName(String property) {
        return "get" + Character.toUpperCase(property.charAt(0)) + property.substring(1);
    }


    /**
     * 根据property返回getXXX method name
     * @param property
     * @return
     */
    public static String returnSetMethodName(String property) {
        return "set" + Character.toUpperCase(property.charAt(0)) + property.substring(1);
    }
}
